<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" destroy-on-close @closed="$emit('closed')" :close-on-press-escape="mode != 'show' ? false : true">
		<el-container>
			<el-header>
				<div class="left-panel">
					<div style="font-size: 16px;">选择打印模板：</div>
					<el-select v-model="form.template_id" clearable filterable placeholder="请选择模板" @change="templateChange">
						<el-option v-for="item in templateList" :key="item.id" :label="item.name" :value="item.id" />
					</el-select>
				</div>
				<div class="right-panel">
					<div class="right-panel-search">
						<el-button type="warning" plain @click="edit">编辑打印</el-button>
						<el-button type="success" plain @click="toPrint">打印</el-button>
						<el-button type="success" plain @click="pdf">下载PDF</el-button>
					</div>
				</div>
			</el-header>
			<el-main>
				<div class="printBoxStyle" style="padding: 50px 20px;margin: 0 auto;" v-html="content" ref="printMain"></div>
			</el-main>
		</el-container>
		<el-footer>
			<el-button @click="visible = false">取 消</el-button>
		</el-footer>
	</el-dialog>
	<edit-dialog v-if="dialog.edit" ref="editDialog" @closed="dialog.edit = false"></edit-dialog>
</template>

<script>
import print from '@/utils/print'
import store from "@/store";
import editDialog from "@/components/scPrintTemplate/edit.vue";
export default {
	emits: ['success', 'closed'],
	components: {
		editDialog
	},
	data() {
		return {
			dialog: {
				edit: false
			},
			mode: "add",
			titleMap: {
				add: '打印',
				edit: '打印',
				show: '打印'
			},
			visible: false,
			isSaveing: false,
			form: {
				id: 1,
				template_id: '',
			},
			templateList: [],
			MenuName: '',
			content: ''
		}
	},
	mounted() {
		let MenuName = store.state.print.MenuName
		if (MenuName) {
			this.MenuName = MenuName
		}
		this.getTemplate()
	},
	beforeUnmount() {

	},
	methods: {
		/**编辑打印点击*/
		edit() {
			this.dialog.edit = true
			this.$nextTick(() => {
				this.$refs.editDialog.open('add').setDataDialog({
					MenuName: this.MenuName,
					...this.form
				})
			})
		},
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		/**模板选中事件*/
		templateChange() {
			this.setData()
		},
		/**获取打印模板*/
		async getTemplate() {
			var res = await this.$API.print.template.all.get({ MenuName: this.MenuName });
			this.templateList = res.data
			if (this.templateList.length > 0) {
				this.form.template_id = this.templateList[0].id
				this.setData()
			}
		},
		//表单注入数据
		async setData() {
			var res = await this.$API.print.template.printContant.get(this.form);
			this.content = res.data.content
		},
		/**获取打印数据id*/
		getId(data) {
			this.form.id = data
		},
		/**打印按钮点击*/
		toPrint() {
			print(this.$refs.printMain)
		},
		/**下载pdf点击*/
		async pdf() {
			let obj = {
				export: 1,
				...this.form
			}
			var res = await this.$API.print.template.printContant.get(obj);
			if (res.code == 1) {
				const fileUrl = res.data.pdf_url;
				window.open(fileUrl, '_blank')
			} else {
				this.$alert(res.message, "提示", { type: "error" });
			}
		},
	}
}
</script>

<style lang="scss">
.el-input-group__append {
	padding: 0 8px;
}

//.printBoxStyle {
//	p {
//		line-height: 20px!important;
//	}
//}</style>
